package com.audaque.springboot.foshanupload.web.esdatademo.controller;

import com.audaque.springboot.foshanupload.web.esdatademo.api.StudentApi;
import com.audaque.springboot.foshanupload.web.esdatademo.repository.StudentRepository;
import com.audaque.springboot.foshanupload.web.esdatademo.model.pojo.StudentPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.stereotype.Controller;

import java.util.*;


/**
 * @Description:用户信息表 管理
 * @Auther:zhong.guobin
 * @DateTime:2023-01-23T02:16:18.507
 */

@Controller
public class StudentAction implements StudentApi {
   /* @Autowired
    private StudentService studentService;
*/

    @Autowired
    private StudentRepository studentRepository;


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

/*    @PostConstruct
    public void init(){
        //默认情况下，springboot data elasticsearch使用如下映射方式，自定义映射不起作用，需要手动添加映射
        elasticsearchRestTemplate.putMapping(StudentPojo.class);

    }*/


    public  void test() {



        List<String> colorList = new ArrayList<>();//颜色
        colorList.add("red");
        colorList.add("white");
        colorList.add("black");
        StudentPojo student1 = new StudentPojo("1", "mhh", "济南", 12, new Date(), new String[]{"语文", "数学", "英语"}, colorList);
        StudentPojo student2 = new StudentPojo("2", "mhh", "济南", 12, new Date(), new String[]{"语文", "数学", "英语"}, colorList);
        StudentPojo save1 = elasticsearchRestTemplate.save(student1);
        StudentPojo save2 = elasticsearchRestTemplate.save(student2);
        System.out.println(save1);
        System.out.println(save2);

        Criteria criteria = new Criteria("sName").is("mhh").and("sAddress").is("济南");
        Query query = new CriteriaQuery(criteria);
        SearchHits<StudentPojo> mapSearchHits = elasticsearchRestTemplate.search(query, StudentPojo.class, IndexCoordinates.of("student"));
        List<SearchHit<StudentPojo>> searchHits = mapSearchHits.getSearchHits();


        HashMap<String, Object> map = new HashMap<>();
        map.put("sName","fff");
        UpdateQuery.Builder builder = UpdateQuery.builder("1").withDocument(Document.from(map));
        UpdateResponse update = elasticsearchRestTemplate.update(builder.build(), IndexCoordinates.of("student"));
        System.out.println(update);

        String delete = elasticsearchRestTemplate.delete("1",IndexCoordinates.of("student"));
        System.out.println(delete);



        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        Optional<StudentPojo> optionalStudent = studentRepository.findById("2");
        System.out.println(optionalStudent.get());
        List<StudentPojo> students = studentRepository.findStudentByName("mhh");
        System.out.println(students);
    }



}
